// Package models
// @Title  articleModel.go
// @Description  文章模型
// @Author  小斌 (时间 格式是2021/12/02  10:20)
// @Update  小斌 (时间 格式是2021/12/02  10:20)

package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

// ArticleObj 文章 实体类
type ArticleObj struct {
	Id                 int    `valid:"id"`
	Title                string `valid:"title"`
	Introduction                string `valid:"introduction"`
	Content				string `valid:"content"`
	CatId				int `valid:"cat_id"`
	Tags				string `valid:"tags"`
	HomeRecommend				int `valid:"home_recommend"`
	BloggerRecommend				int `valid:"blogger_recommend"`
	Author				int `valid:"author"`
	ImageUrl				string `valid:"image_url"`
	Status				int `valid:"status"`
	IsDelete				int `valid:"is_delete"`
	UpdateTime                time.Time `valid:"update_time" orm:"auto_now"`
	AddTime                time.Time `valid:"add_time" orm:"auto_now_add"`
}

type ArticleQueryParams struct{
	PageSize int
	Page int
	CatId int
	Status int
	HomeRecommend int
	BloggerRecommend int
}

func (this *ArticleObj) TableName() string {
	return "article"
}

func init() {
	orm.RegisterModel(new(ArticleObj))
}

// GetArticleInfoById 根据ID获取记录
func GetArticleInfoById(id int) (ArticleObj,error) {
	var (
		row ArticleObj
		err  error
	)

	err = orm.NewOrm().QueryTable(new(ArticleObj)).Filter("id", id).Filter("is_delete", 0).One(&row)
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return row,nil
	}

	return row,nil
}

// GetArticleList 获取分页数据
func GetArticleList(params ArticleQueryParams) (*[]ArticleObj, int, error) {
	var (
		list []ArticleObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(ArticleObj)).OrderBy("-add_time")
	if params.HomeRecommend > 0{
		qs = qs.Filter("home_recommend", params.HomeRecommend)
	}
	if params.CatId > 0{
		qs = qs.Filter("cat_id", params.CatId)
	}

	_, err = qs.Filter("is_delete",0).Limit(params.PageSize).Offset((params.Page - 1) * params.PageSize).RelatedSel().All(&list)
	count, _ := qs.Filter("is_delete",0).Count()

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return nil, 0, err
	}

	return &list, int(count), nil
}

// AddArticle 添加文章
func AddArticle(title string,author int,introduction string,catId int,tags string,imageUrl string,homeRecommend int,bloggerRecommend int,contents string,status int) int64 {
	article := ArticleObj{
		Title:    title,
		Author: author,
		Introduction: introduction,
		CatId: catId,
		Tags: tags,
		ImageUrl: imageUrl,
		HomeRecommend: homeRecommend,
		BloggerRecommend: bloggerRecommend,
		Content: contents,
		Status: status,
		IsDelete: 0,
	}

	id,err := orm.NewOrm().Insert(&article)

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return id
}

// UpdateArticleById 修改文章
func UpdateArticleById(id int,title string,introduction string,catId int,tags string,imageUrl string,homeRecommend int,bloggerRecommend int,content string,status int) int64 {
	article := ArticleObj{
		Id:		id,
		Title:    title,
		Introduction: introduction,
		CatId: catId,
		Tags: tags,
		ImageUrl: imageUrl,
		HomeRecommend: homeRecommend,
		BloggerRecommend: bloggerRecommend,
		Content: content,
		Status: status,
	}
	num,err := orm.NewOrm().Update(&article,"title","introduction","catId","tags","imageUrl","homeRecommend","bloggerRecommend","content","status")

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}

// DeleteArticleById 删除文章
func DeleteArticleById(id int) int64 {
	article := ArticleObj{
		Id:    id,
		IsDelete: 1,
	}
	num,err := orm.NewOrm().Update(&article,"is_delete")
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return 0
	}

	return num
}